package edu.guigu.networkdemo.single;

import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import edu.guigu.networkdemo.network.GithubClient;
import edu.guigu.networkdemo.databinding.ActivitySingleViewBinding;
import edu.guigu.networkdemo.model.User;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SingleViewActivity extends AppCompatActivity {

    private ActivitySingleViewBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);

        binding = ActivitySingleViewBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        ViewCompat.setOnApplyWindowInsetsListener(binding.main, (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        GithubClient githubClient = GithubClient.getInstance();
        retrofit2.Call<User> call = githubClient.getUser("5");

        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                User user = response.body();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        binding.itemUser.tvName.setText(user.getName());
                        binding.itemUser.tvCreatedAt.setText(user.getCreated_at());
                        binding.itemUser.tvUpdatedAt.setText(user.getUpdated_at());
                        // 加载网络图像，设置圆角，采用Glide
                        String imageUrl = user.getAvatar_url();
                        Glide.with(getApplication())
                                .load(imageUrl)
                                .apply(new RequestOptions().transform(new RoundedCorners(12))) //圆角图片
                                .into(binding.itemUser.ivAvatar);
                    }
                });


            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {

            }
        });

//       Call call = UserClient.getInstance().getUser("https://api.github.com/users/5");
        // 异步执行这一次call
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(@NonNull Call call, @NonNull IOException e) {
//                runOnUiThread(() -> {
//                    Toast.makeText(SingleViewActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
//                });
//            }
//
//            @Override
//            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
//                // 从响应中获取数据（JSON格式）
//                String jsonString = response.body().string();
//                Log.d("TAG", "onResponse: " + jsonString);
//                // UI线程刷新视图
//                runOnUiThread(() -> {
//                    Toast.makeText(SingleViewActivity.this, "请求成功", Toast.LENGTH_SHORT).show();
//                    // 用JSON解析工具GSON，进行反序列化(JSON字符串 -> Java对象)
//                    Gson gson = new Gson();
//                    User user = gson.fromJson(jsonString, User.class);
//                    binding.itemUser.tvName.setText(user.getName());
//                    binding.itemUser.tvCreatedAt.setText(user.getCreated_at());
//                    binding.itemUser.tvUpdatedAt.setText(user.getUpdated_at());
//                    // 加载网络图像，设置圆角，采用Glide
//                    String imageUrl = user.getAvatar_url();
//                    Glide.with(getApplication())
//                            .load(imageUrl)
//                            .apply(new RequestOptions().transform(new RoundedCorners(12))) //圆角图片
//                            .into(binding.itemUser.ivAvatar);
//                });
//            }
//        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
    }
}